package rent.hj.videoplayer.uitl;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

/**
 * Created by wangjianqiang on 2016/11/1.
 */
public class LibSwitchHelper {
    public static final int RMVB_LIB = 0;
    public static final int ISO_LIB = 1;

    public static String getMd5ByFile(File file) {
        String value = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

    public static boolean checkLib(int need) {
        String currentMd5 = getMd5ByFile(new File("/system/lib/librkffplayer.so"));
        String needMd5 = null;
        switch (need) {
            case RMVB_LIB:
                needMd5 = getMd5ByFile(new File("/vendor/rkffplayer/rmvb/lib/librkffplayer.so"));
                break;
            case ISO_LIB:
                needMd5 = getMd5ByFile(new File("/vendor/rkffplayer/common/lib/librkffplayer.so"));
                break;
        }
        if (currentMd5 == null || needMd5 == null) {
            return false;
        }
        return currentMd5.equals(needMd5);
    }

    public static void restartMediaServer() {
        int pid = 0;
        try {
            ProcessBuilder pb = new ProcessBuilder();
            pb.command("ps");
            Process p = pb.start();
            InputStream ii = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(ii));
            String line = "";
            line = reader.readLine();
            while ((line = reader.readLine()) != null) {
                String[] strs = line.split("[ ]+");
                if (strs[8].equals("/system/bin/mediaserver")) {
                    pid = Integer.parseInt(strs[1]);
                    break;
                }
            }
            ii.close();
            if (pid != 0) {
                android.os.Process.killProcess(pid);
            }
        } catch (Exception e) {
            Log.e("fuck you",e.toString()+"restartMediaServer");
        }
    }
    public static boolean mediaServerHasStarted(){
        try {
            ProcessBuilder pb = new ProcessBuilder();
            pb.command("ps");
            Process p = pb.start();
            InputStream ii = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(ii));
            String line = "";
            line = reader.readLine();
            Log.e("fuck you",line+"ssssss");
            while ((line = reader.readLine()) != null) {
                Log.e("fuck you",line+"ssssss");
                String[] strs = line.split("[ ]+");
                if (strs[8].equals("/system/bin/mediaserver")) {
                    return true;
                }
            }
            ii.close();

        } catch (Exception e) {
            Log.e("fuck you",e.toString()+"restartMediaServer");
        }
        return false;
    }
}
